#pragma once

#include<BWAPI.h>
#include<vector>

#include"DragoonControler.h"
#include"ZealotControler.h"
#include"HydraliskControler.h"

using namespace BWAPI;

using std::vector;

class UnitControler : public AIModule
{
public:
	UnitControler()
		:_started(false),
		_initialized(false),
		_probe(NULL)
	{
	}
	virtual void onStart()
	{
		Broodwar->setLocalSpeed(500);
		Broodwar->enableFlag(Flag::UserInput);
		//Broodwar->enableFlag(Flag::CompleteMapInformation);
		_started = true;
	}
	virtual void onFrame()
	{
		if(_started)
		{
			if(!_initialized)
			{
				if(Broodwar->getFrameCount() > 1)
				{
					for each(Unit* whichUnit in Broodwar->self()->getUnits())
					{
						if(whichUnit->getType() == UnitTypes::Zerg_Hydralisk)
						{
							_hydraliskControlers.push_back(new HydraliskControler(whichUnit));
						}
					}

					_initialized = true;
				}
			}
			else
			{
				Broodwar->drawTextScreen( 2 , 2 , "Frame count: %d" , Broodwar->getFrameCount());
				if(_probe == NULL || _probe->getHitPoints() <= 0 || !_probe->exists())
				{
					for each(Unit* u in Broodwar->enemy()->getUnits())
					{
						if(u->getHitPoints() > 0)
						{
							_probe = u;
							break;
						}
					}
				}
				/*
				for each(HydraliskControler* z in _hydraliskControlers)
				{
				z->DoAttackChase(_probe);
				}
				break;
				}
				}
				}*/

				for each(HydraliskControler* h in _hydraliskControlers)
				{
					if(h->IsIdle())
					{
						h->AttackMoveTo(Position(1000 , 200));
					}
					else
					{
						if(_probe != NULL && _probe->exists())
						{
							if(h->CanAttack())
							{
								h->Attack(_probe);
							}
							else
							{
								if(h->CanMove())
								{
									h->Move(_probe->getPosition());
								}
							}
						}
					}
					//h->DoAttackChase(_probe);
					h->OnFrame();
				}
			}
		}
	}
private:
	bool _started;
	bool _initialized;
	Unit* _probe;
	vector<HydraliskControler*> _hydraliskControlers;
};